package sdk.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import dao.ContractDAO;
import dao.impl.ContractDAOImpl;
import dao.impl.DutyDAOImpl;
import dao.impl.EmpDAOImpl;

import model.Contract;
import util.Page;

public class ContractManager {
	ContractDAO cd = new ContractDAOImpl();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

	public Page<Contract> getAllContract(int pageNo, int pageSize) {

		return cd.getAllContract(pageNo, pageSize);
	}

	public Contract getById(int id) {

		return cd.getById(id);
	}

	public Contract getById(String conno) {
		return cd.getById(conno);
	}

	public Page<Contract> getContractByTime(int pageNo, int pageSize,
			Date starttime, Date endtime) {

		return cd.getContractByTime(pageNo, pageSize, starttime, endtime);
	}

	public Page<Contract> getContractByContent(int pageNo, int pageSize,
			String content) {
		return cd.getContractByContent(pageNo, pageSize, content);

	}

	public boolean addContract(String empno, String contractno, String starttime,
			String endtime, int dutyid, String content, String supplement) {
		Contract contract = new Contract();
		contract.setEmp(new EmpDAOImpl().getEmpById(new EmpDAOImpl().getIDByEmpno(empno)));
		contract.setContractno(contractno);
		try {
			contract.setStarttime(sdf.parse(starttime));
			contract.setEndtime(sdf.parse(endtime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		contract.setDuty(new DutyDAOImpl().getDutyById(dutyid));
		contract.setContent(content);
		contract.setSupplement(supplement);
		
		return cd.addContract(contract);
	}
	
	public boolean updateContract(int id,String empno,String contractno,String starttime,
			String endtime,int dutyid,String content,String supplement) {
		Contract contract=new Contract();
		
		contract.setId(id);
		contract.setEmp(new EmpDAOImpl().getEmpById(new EmpDAOImpl().getIDByEmpno(empno)));
		contract.setContractno(contractno);
		try {
			contract.setStarttime(sdf.parse(starttime));
			contract.setEndtime(sdf.parse(endtime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		contract.setDuty(new DutyDAOImpl().getDutyById(dutyid));
		contract.setContent(content);
		contract.setSupplement(supplement);
		
		return cd.updateContract(contract);
	}

	public boolean deleteContract(Contract contract) {
		return cd.deleteContract(contract);
	}

	public boolean deleteById(int id) {

		return cd.deleteById(id);
	}

	public boolean deleteByConno(int conno) {

		return cd.deleteByConno(conno);
	}

	public Page<Contract> getByType(int pageNo, int pageSize, Object object) {
		return cd.getByType(pageNo, pageSize, object);
	}
}
